package com.jenkin.jcache.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期时间格式化
 * @author yangjie
 *
 */
public class DateTimeTools {
	
	private static final SimpleDateFormat dateAll = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final SimpleDateFormat dateShort = new SimpleDateFormat("yyyyMMdd");
	private static final SimpleDateFormat dateYearMonthDay = new SimpleDateFormat("yyyy-MM-dd");
	private static final SimpleDateFormat dateYearMonthDayForChinese = new SimpleDateFormat("yyyy年MM月dd日");
	private static final SimpleDateFormat dateYearMonthDayForSlash = new SimpleDateFormat("yyyy / MM / dd");
	private static final SimpleDateFormat dateNoSecond = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	
	/**
	 * 返回年月日
	 * @param date
	 * @return
	 */
	public static String formatDateShort(Date date) {
		if (date == null){
			return "00000000";
		}
		synchronized(dateShort){
			return dateShort.format(date);
		}
	}
	
	/**
	 * 返回中文的年月日
	 * @param date
	 * @return
	 */
	public static String formatDateYearMonthDayForChinese(Date date) {
		if (date == null){
			return "0000年00月00日";
		}
		synchronized(dateYearMonthDayForChinese){
			return dateYearMonthDayForChinese.format(date);
		}
	}
	
	/**
	 * 返回正斜杠分割的年月日
	 * @param date
	 * @return
	 */
	public static String dateYearMonthDayForSlash(Date date) {
		if (date == null){
			return "0000 / 00 / 00";
		}
		synchronized(dateYearMonthDayForSlash){
			return dateYearMonthDayForSlash.format(date);
		}
	}
	
	/**
	 * 返回标准的年月日
	 * @param date
	 * @return
	 */
	public static String formatDateYearMonthDay(Date date) {
		if (date == null){
			return "0000-00-00";
		}
		synchronized(dateYearMonthDay){
			return dateYearMonthDay.format(date);
		}
	}
	
	/**
	 * 不返回秒
	 * @param date
	 * @return
	 */
	public static String formatDateNoSecond(Date date) {
		if (date == null){
			return "0000-00-00 00:00";
		}
		synchronized(dateNoSecond){
			return dateNoSecond.format(date);
		}
	}
	
	/**
	 * 完整的时间
	 * @param date
	 * @return
	 */
	public static String formatDate(Date date) {
		if (date == null){
			return "0000-00-00 00:00:00";
		}
		synchronized(dateAll){
			return dateAll.format(date);
		}
	}
	
	/**
	 * 把字符串转为Date
	 * @param strDateTime
	 * @return
	 */
	public static Date getDateTime(String strDateTime){
		if(strDateTime == null){
			return null;
		}
		try {
			synchronized(dateAll){
				return dateAll.parse(strDateTime);
			}
		} catch (ParseException e) {
			e.printStackTrace();
			return new Date();
		}
	}
	
	/**
	 * 判断时间格式是否正确
	 * @param strDateTime
	 * @return
	 */
	public static boolean isDateTime(String strDateTime){
		try{
			synchronized(dateAll){
				dateAll.parse(strDateTime);
			}
		}catch (Exception e) {
			return false;
		}
		return true;
	}
	
	/**
	 * 获取指定日期的毫秒
	 * @param p_date util.Date日期
	 * @return long 毫秒
	 */
	public static long getMillisOfDate(java.util.Date p_date) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(p_date);
		return c.getTimeInMillis();
	}
	
	/**
	 * 获取2个字符日期的分钟差
	 * @param p_startDate
	 * @param p_endDate
	 * @return 天数差
	 */
	public static long getMinuteOfTowDiffDate(String p_startDate, String p_endDate)
			throws ParseException {
		Date l_startDate = getDateTime(p_startDate);
		Date l_endDate = getDateTime(p_endDate);
		long l_startTime = getMillisOfDate(l_startDate);
		long l_endTime = getMillisOfDate(l_endDate);
		long betweenDays = (long) ((l_endTime - l_startTime)/(1000*60));
		return betweenDays;
	}
	
	/**
	 * 获取两个日期相差的天数
	 * @param to
	 * @param from
	 * @return
	 */
	public static long discrepancyOfDay(Date to, Date from){
		try {
			long lTo = to.getTime();
		    long lFrom = from.getTime();
		    return (lTo - lFrom) / (1000 * 60 * 60 * 24);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	
}
